﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentJump : Component, IUpdateable
    {
        public float m_BlackoutDuration;

        public float m_BlackoutFactor;

        public float Jump_first;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public float Jump_firstR;

        public float m_JumpRate;

        public float m_JumpDuration;

        public Random m_random = new Random();

        public float JumpRate => m_JumpRate;

        public bool IsActive => m_JumpDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartJumpBuff(float time, float rate)
        {
            m_JumpRate = rate;
            m_JumpDuration = time;
        }

        public void StopBuff()
        {
            if (m_componentPlayer != null)
            {
                m_JumpDuration = 0f;
                m_componentPlayer.ComponentGui.DisplaySmallMessage("跳跃强化结束！", Color.Black, blinking: false, playNotificationSound: false);
                m_componentPlayer.ComponentHealth.FallResilience = Jump_first;
                m_componentPlayer.ComponentLocomotion.JumpSpeed = Jump_firstR;
                m_JumpRate = 0f;
                Jump_first = 0f;
                Jump_firstR = 0f;
            }
        }

        public void Update(float dt)
        {
            if (m_JumpDuration > 0f && JumpRate > 0f)
            {
                if (Jump_first == 0f)
                {
                    Jump_firstR = m_componentPlayer.ComponentLocomotion.JumpSpeed;
                    Jump_first = m_componentPlayer.ComponentHealth.FallResilience;
                    m_componentPlayer.ComponentHealth.FallResilience = 100000f;
                }

                m_componentPlayer.ComponentLocomotion.JumpSpeed = 15f;
                m_JumpDuration -= dt;
                if (m_JumpDuration <= 0f)
                {
                    StopBuff();
                }
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            Jump_first = valuesDictionary.GetValue("JumpFirst", 0f);
            m_JumpDuration = valuesDictionary.GetValue("JumpDuration", 0f);
            m_JumpRate = valuesDictionary.GetValue("JumpRate", 0f);
            Jump_firstR = valuesDictionary.GetValue("JumpR", 0f);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("JumpFirst", Jump_first);
            valuesDictionary.SetValue("JumpDuration", m_JumpDuration);
            valuesDictionary.SetValue("JumpRate", m_JumpRate);
            valuesDictionary.SetValue("JumpR", Jump_firstR);
        }
    }
}